package com.zds.support.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDialog;

import com.zds.support.R;
import com.zds.support.util.ScreenUtils;


public class CommonDialog extends AppCompatDialog implements View.OnClickListener {
    private Context context;
    private TextView tvTitle;
    private TextView tvMsg;
    private TextView btnNegative;
    private TextView btnPositive;
    private boolean dismissWhenClick = true;
    private boolean dismissWhenBackClick = true;
    /*  private String title;
      private String message;
      private boolean isSingleButton = false;
      private String positiveButton;
      private String negativeButton;*/
    private Builder builder;
    private OnClickListener onClickListener;
    private OnLongClickListener onLongClickListener;
    private Integer mNegativeColor = Color.parseColor("#433856");
    private Integer mPositiveColor = Color.parseColor("#FFFFFF");
    public static int UI_FLAGS =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

    public CommonDialog(Context context) {
        super(context, R.style.Support_Dialog);
        this.context = context;
    }

    private CommonDialog(Builder builder) {
        this(builder.context);
        this.builder = builder;
      /*  title = builder.title;
        message = builder.message;
        isSingleButton = builder.isSingleButton;
        positiveButton = builder.positiveButton;
        negativeButton = builder.negativeButton;*/
        onClickListener = builder.onClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.support_dialog_common);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvMsg = (TextView) findViewById(R.id.tvMsg);
        btnNegative = (TextView) findViewById(R.id.btnNegative);
        btnPositive = (TextView) findViewById(R.id.btnPositive);


        btnNegative.setOnClickListener(this);
        btnPositive.setOnClickListener(this);
        btnPositive.setOnLongClickListener(onViewLongClickListener);
        btnNegative.setOnLongClickListener(onViewLongClickListener);
        btnPositive.setOnClickListener(this);
        if (builder != null) {
            setTitle(builder.title);

            setMessage(builder.message);
            if (builder.isSingleButton) {
                setSingleButtonText(builder.positiveButton);
            } else {
                setButtonText(builder.positiveButton, builder.negativeButton);
            }
            setMessageGravity(builder.messageGravity);
            setDismissWhenBackClick(builder.dismissWhenBackClick);
            setDismissWhenClick(builder.dismissWhenClick);
            setOnClickListener(builder.onClickListener);
            setCanceledOnTouchOutside(builder.canceledOnTouchOutside);
            setCancelable(builder.cancelable);
            //是否修改颜色
            if (builder.mNegativeColor != null) {
                mNegativeColor = builder.mNegativeColor;
            }
            if (builder.mPositiveColor != null) {
                mPositiveColor = builder.mPositiveColor;
            }
        }
        btnNegative.setTextColor(mNegativeColor);
        btnPositive.setTextColor(mPositiveColor);
        Resources resources = context.getResources();
        Configuration mConfiguration = resources.getConfiguration(); //获取设置的配置信息
        int ori = mConfiguration.orientation; //获取屏幕方向
        if (ori == Configuration.ORIENTATION_LANDSCAPE) {
            Window dialogWindow = getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            boolean isPad = false;
            if (isPad) {
                lp.width = resources.getDimensionPixelSize(R.dimen.dp_358);
            } else {
                int screenWidth = ScreenUtils.getScreenWidth(context);
                lp.width = (int) (screenWidth * 0.5);
            }

            dialogWindow.setAttributes(lp);
        }
    }

    private View.OnLongClickListener onViewLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (onLongClickListener != null) {
                boolean isPositive = false;
                if (v != null && v.getId() == R.id.btnPositive) {
                    isPositive = true;
                }
                return onLongClickListener.onLongClick(CommonDialog.this, isPositive);
            }
            return false;
        }
    };

    private float dp2px(Float dpVal) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }

    public void setTitle(CharSequence title) {
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.VISIBLE);

        }
        tvTitle.setText(title);
    }

    public void setDismissWhenClick(boolean dismissWhenClick) {
        this.dismissWhenClick = dismissWhenClick;
    }

    public void setDismissWhenBackClick(boolean dismissWhenBackClick) {
        this.dismissWhenBackClick = dismissWhenBackClick;
    }

    public void setTitle(@StringRes int titleId) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText(titleId);
    }

    public void setMessage(@NonNull String message) {
        if (TextUtils.isEmpty(message)) {
            tvMsg.setVisibility(View.GONE);
        } else {
            tvMsg.setVisibility(View.VISIBLE);
            tvMsg.setText(message);
        }

    }

    public void setMessage(@StringRes int messageId) {
        tvMsg.setText(messageId);
    }

    public void setButtonText(@NonNull String positiveButton, @NonNull String negativeButton) {
        btnNegative.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(positiveButton)) {
            btnPositive.setText(positiveButton);
        }
        if (!TextUtils.isEmpty(negativeButton)) {
            btnNegative.setText(negativeButton);
        }

        btnPositive.setBackgroundResource(R.drawable.support_btn_dialog_common_positive);
    }

    public void setButtonText(@StringRes int positiveButtonId, @StringRes int negativeButtonId) {
        btnNegative.setVisibility(View.VISIBLE);
        btnPositive.setText(positiveButtonId);
        btnNegative.setText(negativeButtonId);
        btnPositive.setBackgroundResource(R.drawable.support_btn_dialog_common_positive);
    }

    public void setMessageGravity(int gravity) {
        tvMsg.setGravity(gravity);
    }

    public void setSingleButtonText(@NonNull String buttonText) {
        btnNegative.setVisibility(View.GONE);
        //    btnPositive.setText(buttonText);
        if (!TextUtils.isEmpty(buttonText)) {
            btnPositive.setText(buttonText);
        }
        btnPositive.setBackgroundResource(R.drawable.support_btn_dialog_common_single);
    }

    public void setSingleButtonText(@StringRes int buttonTextId) {
        btnNegative.setVisibility(View.GONE);
        btnPositive.setText(buttonTextId);
        btnPositive.setBackgroundResource(R.drawable.support_btn_dialog_common_single);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnPositive) {
            if (dismissWhenClick) {
                dismiss();
            }

            if (onClickListener != null) {
                onClickListener.onClick(this, true);
            }
        } else if (id == R.id.btnNegative) {
            if (dismissWhenClick) {
                dismiss();
            }
            if (onClickListener != null) {
                onClickListener.onClick(this, false);
            }
        }


    }

    @Override
    public void show() {
        try {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            getWindow().getDecorView().setSystemUiVisibility(UI_FLAGS);
        } catch (Exception e) {
        }

        super.show();
        try {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        } catch (Exception e) {
        }
    }

    @Override
    public void onBackPressed() {
        if (dismissWhenBackClick) {
            super.onBackPressed();
        }

    }

    public interface OnClickListener {
        void onClick(DialogInterface dialog, boolean isPositive);
    }

    public interface OnLongClickListener {
        boolean onLongClick(DialogInterface dialog, boolean isPositive);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message = "";
        private String positiveButton = "";
        private String negativeButton = "";
        private Resources resources;
        private boolean isSingleButton = false;
        private boolean canceledOnTouchOutside = true;
        private boolean cancelable = true;
        private boolean dismissWhenClick = true;
        private boolean dismissWhenBackClick = true;
        private OnClickListener onClickListener;
        private int messageGravity = Gravity.LEFT;
        private Integer mNegativeColor;
        private Integer mPositiveColor;
        private OnLongClickListener onLongClickListener;

        public Builder(@NonNull Context context) {
            this.context = context;
            resources = context.getResources();
        }

        public Builder setMessageGravity(int gravity) {
            messageGravity = gravity;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTitle(@StringRes int titleId) {
            this.title = resources.getString(titleId);
            return this;
        }

        public Builder setDismissWhenClick(boolean dismissWhenClick) {
            this.dismissWhenClick = dismissWhenClick;
            return this;
        }

        public Builder setDismissWhenBackClick(boolean dismissWhenBackClick) {
            this.dismissWhenBackClick = dismissWhenBackClick;
            return this;
        }

        public Builder setMessage(@NonNull String message) {
            this.message = message;
            return this;
        }

        public Builder setCancelable(boolean cancel) {
            this.cancelable = cancel;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean cancel) {
            this.canceledOnTouchOutside = cancel;
            return this;
        }

        public Builder setMessage(@StringRes int messageId) {
            this.message = resources.getString(messageId);
            return this;
        }

        /**
         * @param positiveButton 右边按钮文字
         * @param negativeButton 左边按钮文字
         */
        public Builder setButtonText(@NonNull String positiveButton, @NonNull String negativeButton) {
            isSingleButton = false;
            this.positiveButton = positiveButton;
            this.negativeButton = negativeButton;
            return this;
        }

        public Builder setButtonText(@StringRes int positiveButtonId, @StringRes int negativeButtonId) {
            this.positiveButton = resources.getString(positiveButtonId);
            this.negativeButton = resources.getString(negativeButtonId);
            return this;
        }

        public Builder setSingleButtonText(@NonNull String buttonText) {
            isSingleButton = true;
            this.positiveButton = buttonText;
            return this;
        }

        public Builder setSingleButtonText(@StringRes int buttonTextId) {
            isSingleButton = true;
            this.positiveButton = resources.getString(buttonTextId);
            return this;
        }

        public Builder setOnClickListener(OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
            return this;
        }

        public Builder setOnLongClickListener(OnLongClickListener onLongClickListener) {
            this.onLongClickListener = onLongClickListener;
            return this;
        }

        public Builder setNegativeColor(Integer color) {
            this.mNegativeColor = color;
            return this;
        }

        public Builder setPostiveColor(Integer color) {
            this.mPositiveColor = color;
            return this;
        }

        public CommonDialog create() {
            return new CommonDialog(this);
        }

        public CommonDialog show() {
            CommonDialog dialog = new CommonDialog(this);
            dialog.show();
            return dialog;
        }


    }
}
